package cn.kgc.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author wangyue
 * @version 1.0
 */
@Slf4j
@Configuration
@SuppressWarnings("all")
public class RabbitConsumerConfig {
    @Value("${mq.prefetchCount}")
    private int prefetchCount;

    @Value("${mq.concurrentConumsers}")
    private int concurrentConumsers;

    @Value("${mq.maxConcurrentConsumers}")
    private int maxConcurrentConsumers;

    @Bean
    public RabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory){
        SimpleRabbitListenerContainerFactory simpleRabbitListenerContainerFactory=
                new SimpleRabbitListenerContainerFactory();
        //手动消费信息
        simpleRabbitListenerContainerFactory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        //设置每次消费信息的数量
        simpleRabbitListenerContainerFactory.setPrefetchCount(prefetchCount);
        //设置多个并发消费可以一起执行
        simpleRabbitListenerContainerFactory.setConcurrentConsumers(concurrentConumsers);
        simpleRabbitListenerContainerFactory.setMaxConcurrentConsumers(maxConcurrentConsumers);
        //设置连接工厂
        simpleRabbitListenerContainerFactory.setConnectionFactory(connectionFactory);
        return simpleRabbitListenerContainerFactory;
    }
}
